package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.constant.RedisKeyFactory;
import cn.com.duiba.millionaire.center.api.dto.MillionaireQuestionBagDto;
import cn.com.duiba.millionaire.center.api.dto.MillionaireQuestionDto;
import cn.com.duiba.millionaire.center.api.requestParam.MillionaireQuestionBagRequestParam;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireActivityDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireQuestionBagDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireQuestionDao;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireActivityEntity;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireQuestionBagEntity;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireQuestionEntity;
import cn.com.duiba.millionaire.center.biz.service.MillionaireQuestionService;
import cn.com.duiba.wolf.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by sty on 1/22/18.
 */
@Service
public class MillionaireQuestionServiceImpl implements MillionaireQuestionService {

    @Autowired
    private MillionaireQuestionBagDao millionaireQuestionBagDao;
    @Autowired
    private MillionaireQuestionDao millionaireQuestionDao;
    @Autowired
    private MillionaireActivityDao millionaireActivityDao;
    @Resource(name="redisTemplate")
    private RedisTemplate<String,MillionaireQuestionDto> redisTemplate;


    @Override
    public Integer countMillionaireQuestionBagBackendList(MillionaireQuestionBagRequestParam param) {
        return millionaireQuestionBagDao.findByPageCount(param);
    }

    @Override
    public List<MillionaireQuestionBagDto> getMillionaireQuestionBagList(MillionaireQuestionBagRequestParam param) {
        return BeanUtils.copyList(millionaireQuestionBagDao.findByPage(param), MillionaireQuestionBagDto.class);
    }

    @Override
    public Boolean saveOrUpdateMillionaireQuesionBag(MillionaireQuestionBagDto dto) {
        if(dto.getId()==null){
            dto.setQuestionNum(0L);
            MillionaireQuestionBagEntity result = millionaireQuestionBagDao.insert(BeanUtils.copy(dto, MillionaireQuestionBagEntity.class));
            if(result == null || result.getId()==null){
                return false;
            }
            return true;
        }else{
            int ret = millionaireQuestionBagDao.updateById(BeanUtils.copy(dto, MillionaireQuestionBagEntity.class));
            if(ret > 0){
                return true;
            }
            return false;
        }
    }

    @Override
    public List<MillionaireQuestionDto> findAllMillionaireQuesionByBagId(Long id) {
        return BeanUtils.copyList(millionaireQuestionDao.findByBagId(id), MillionaireQuestionDto.class);
    }

    @Override
    public Boolean saveOrUpdateMillionaireQuesion(MillionaireQuestionDto dto) {
        //更新操作
        if (dto.getId() != null) {
            return millionaireQuestionDao.updateById(BeanUtils.copy(dto, MillionaireQuestionEntity.class)) > 0;
        }
        
        if (dto.getQuestionBagId() == null) {
            return false;
        }
        MillionaireQuestionEntity question = millionaireQuestionDao.insert(BeanUtils.copy(dto, MillionaireQuestionEntity.class));
        if (question != null && question.getId() != null) {
            //新增成功更改对应的题库
            MillionaireQuestionBagEntity bag = millionaireQuestionBagDao.findById(question.getQuestionBagId());
            if (bag != null) {
                millionaireQuestionBagDao.changeQuestionNum(bag.getId(), 1);
            }
            return true;
        }
        return false;

    }

    @Override
    public Boolean delete(Long id, Integer type) {
        int ret = 0;
        if(type == 2){
            ret = millionaireQuestionDao.deleteById(id);
            if(ret > 0){
                updateQuestionBagWhenDelete(id);
            }
        }
        if(type == 1) {
            ret = millionaireQuestionBagDao.deleteById(id);
        }
        return ret > 0;
    }

    private void updateQuestionBagWhenDelete(Long id) {
        MillionaireQuestionEntity question = millionaireQuestionDao.findById(id);
        if(question==null){
            return;
        }
        MillionaireQuestionBagEntity bag = millionaireQuestionBagDao.findById(question.getQuestionBagId());
        if(bag==null) {
            return;
        }
        millionaireQuestionBagDao.changeQuestionNum(bag.getId(), -1);
    }

    @Override
    public MillionaireQuestionDto findQuesionById(Long id) {
        return BeanUtils.copy(millionaireQuestionDao.findById(id), MillionaireQuestionDto.class);
    }

    @Override
    public MillionaireQuestionDto findQuestionByActivityIdAndIndex(Long activityId, Integer queryIndex) {

        MillionaireQuestionDto dto = (MillionaireQuestionDto)redisTemplate.boundHashOps(buildKey(activityId)).get(String.valueOf(queryIndex));
        if(null!=dto){
            return dto;
        }
        MillionaireActivityEntity activityEntity = millionaireActivityDao.findById(activityId);
        if(null==activityEntity){
            return null;
        }
        List<MillionaireQuestionEntity> list = millionaireQuestionDao.findByBagId(activityEntity.getQuestionBagId());
        int index = 1;
        int totalNum = activityEntity.getQuestionNum().intValue();
        for(MillionaireQuestionEntity entity:list){
            MillionaireQuestionDto questionDto = BeanUtils.copy(entity,MillionaireQuestionDto.class);
            questionDto.setLastOne(index == totalNum);
            redisTemplate.boundHashOps(buildKey(activityId)).put(String.valueOf(index), questionDto);
            redisTemplate.boundHashOps(buildKey(activityId)).expire(1, TimeUnit.HOURS);
            index ++ ;
        }
        return (MillionaireQuestionDto)redisTemplate.boundHashOps(buildKey(activityId)).get(String.valueOf(queryIndex));
    }

    private String buildKey(Long activityId) {
        return RedisKeyFactory.K009.toString() + activityId;
    }
}
